<?php

ini_set('display_errors', 1);
error_reporting(-1);
require_once '/data/wwwroot/g_vendor/autoload.php';
ini_set('memory_limit', '512M');

use Doraemon\service\UserPortrait\UserPortraitApi;


$uid = $argv[1] ?? 0;
$debug = $argv[2] ?? 0;

$keywords = [
    14 => '步川',
    16 => '非左即右',
    15 => '淘淘适家',
    18 => '佐敦涂料',
//    '三只喜鹊',
    19 => '戴露地板',
    20 => '罗浮威尔',
    21 => '意风家居',
    17 => '阿贝尼',
//    '观隅',
//    'TT微水泥',
    23 => '廓空间',
    24 => '佛罗伦萨',
    25 => '德盾',
    26 => '飞美',
//    '普瑞凡',
    36 => '檀香山',
//    '磐多磨',
    28 => '沃根',
    42 => '微沃',
    35 => '天地华承',
    43 => '帕斯沃',
    27 => '埃森纳赫',
    44 => '京艺华美',
    38 => '柏迪',
    29 => '德尔堡',
//    '芬筑',
    37 => '富沥美嘉',
    30 => '槿木坊',
    31 => '盖泽',
    32 => '飞宇',
    33 => '森林花园',
    34 => '春秋门窗',
];


$uidMap = [];
foreach ($keywords as $storeId => $word) {
    try {
        $rs = getObj($word);
        $uids = array_column($rs, 'uid');
        $uids = array_flip(array_flip($uids));
        $allowUids = [];
        foreach ($uids as $uid) {
            if (isset($uidMap[$uid])) continue;
            $userprofile = UserPortraitApi::requestFromBrand($uid, ['area']);
            if (empty($userprofile['area'])) {
                if ($debug) echo "uid:$uid error area", PHP_EOL;
                continue;
            }
            $uidMap[$uid] = $userprofile['area'];
            if (strstr($userprofile['area'], '北京') != false) $allowUids[] = $uid;
        }
        $realIds = [];
        foreach ($rs as $v) {
            if (in_array($v['uid'], $allowUids)) {
                $realIds[] = $v['id'];
                try {
                    \Biz\service\Store\StoreContentSync::createMainRelation($v['id'], $storeId);
                } catch (\Exception $e2) {
                    echo $word, ' error:', $e2->getMessage(), PHP_EOL;
                }
            }
        }
        echo $word, "obj total:", count($rs), " | real obj total:", count($realIds), "\t", implode(',', $realIds), PHP_EOL;
    } catch (\Exception $e) {
        echo $word, ' error:', $e->getMessage(), PHP_EOL;
    }
}


function getObj($keywords): array
{
    $keywords = is_array($keywords) ? $keywords : [$keywords];
    $where = [
        'content' => $keywords
    ];
    $size = 100;
    $p = 1;
    $scrollId = '';
    $ids = [];
    $compareTotal = 0;
    while (true) {
        $offset = $p >= 1  ? ($p - 1) * $size : 0;
        $minimax = $offset + $size;
        $where["LIMIT"] = [$offset, $size];
        $rs = \Doraemon\model\Es\MainContent::scroll('id,uid', $where, $scrollId);
        if (!isset($rs['total'])) throw new \Exception('scroll data error');
        $total = $rs['total'] ?? 0;
        if ($total && !$compareTotal) $compareTotal = $total;
        $scrollId = $rs['scroll_id'] ?? '';
        $rows = $rs['rows'] ?? [];
        if (!empty($rows)) $ids = array_merge($ids, $rows);
        if ($total <= $minimax) {
            \Doraemon\model\Es\MainContent::clear_scroll($scrollId);
            if ($compareTotal - $size > $total) throw new \Exception('scroll error');
            break;
        }
        $p ++;
    }
    return $ids;
}




